<?php

namespace addons\qingdong\model;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;
use addons\qingdong\model\DailyDraft;
/**
 *工作报告
 */
class Daily Extends Model {
	use SoftDelete;
    // 表名,不含前缀
    protected $name = 'qingdong_daily';
	// 开启自动写入时间戳字段
	protected $autoWriteTimestamp = 'int';
	// 定义时间戳字段名
	protected $createTime = 'createtime';
	protected $updateTime = 'updatetime';
	protected $deleteTime = 'deletetime';

	//获取创建时间
	public function getCreatetimeAttr($value){
		return date('Y-m-d H:i',$value);
	}
    public function getOtherAttr($value){
        return json_decode($value,true);
    }
	//销售
	public function staff() {
		return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,img,name,post');
	}

	//已读
	public function read(){
		return $this->hasMany(DailyRead::class, 'daily_id', 'id')->field('daily_id,createtime');
	}
	/**
	 * @desc 备注
	 * @update_date 2021/6/9 更新时间
	 * @author zhangwei
	 */
	public static function createDaily($params) {
		//自定义字段
		$other = [];
		foreach ($params as $name => $val) {
			if (strstr( $name,'other_') !== false || strstr( $name,'main_') !== false) {
                if(is_array($val)){
                    $other[$name] = implode(',',$val);
                }else{
                    $other[$name] = $val;
                }
				unset($params[$name]);
			}
		}
		$params['other']=json_encode($other);
		$model = new self;
		// 调用当前模型对应的User验证器类进行数据验证
		$result = $model->allowField(true)->save($params);
		if (false === $result) {
			// 验证失败 输出错误信息
			throw new Exception($model->getError());
		}
		$lastId=$model->getLastInsID();

		if (isset($params['reminds_id'])) {//发送通知
			$staff_ids = explode(',', $params['reminds_id']);
			foreach ($staff_ids as $staff_id) {
				//发送通知
				Message::addMessage(Message::DAILY_TYPE, $lastId, $staff_id, $params['create_staff_id']);
			}
		}
		return $lastId;
	}

    /**
     * 修改
     * @param $params
     * @return string
     * @throws Exception
     */
    public static function updateDaily($params) {
        //自定义字段
        $other = [];
        foreach ($params as $name => $val) {
            if (strstr( $name,'other_') !== false || strstr( $name,'main_') !== false) {
                if(is_array($val)){
                    $other[$name] = implode(',',$val);
                }else{
                    $other[$name] = $val;
                }
                unset($params[$name]);
            }
        }
        $params['other']=json_encode($other);
        if(isset($params['reminds_id'])){
            unset($params['reminds_id']);
        }
        $model = new self;
        // 调用当前模型对应的User验证器类进行数据验证
        $result = $model->allowField(true)->save($params,['id'=>$params['id']]);
        if (false === $result) {
            // 验证失败 输出错误信息
            throw new Exception($model->getError());
        }
        $lastId=$model->getLastInsID();

        $where['create_staff_id'] = $params['create_staff_id'];
        $where['type'] = $params['type'];
        DailyDraft::where($where)->update(array('deletetime'=>time()));


        return $lastId;
    }
}
